package com.hspedu.newData.LCR;

import com.hspedu.newData.dmsxl.ds.ListNode;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-17 
 * @Version: 1.0
 */

public class LCR22环形链表Ⅱ {

    public ListNode detectCycle(ListNode head) {

        ListNode dummy = new ListNode(0,head);

        ListNode fast = dummy;
        ListNode slow = dummy;

        while (fast.next!=null && fast.next.next != null){
            fast = fast.next.next;
            slow = slow.next;

            if (slow == fast){

                ListNode cur = dummy;

                while (cur != slow){
                    cur = cur.next;
                    slow = slow.next;
                }

                return cur;

            }
        }

        return null;
    }
}
